package com.loongtech.bi.action.system;

import com.loongtech.bi.entity.system.*;
import com.loongtech.bi.manager.system.*;
import com.loongtech.bi.support.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单个角色信息展示
 * 
 * @author xckuang
 * @date 2014-10-08
 */
@Controller("roleInfoManager")
@Scope("view")
public class RoleInfoManager implements Serializable {
	private static final long serialVersionUID = 1L;
	@Autowired
	private Session session;
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private SysRoleManager sysRoleManager;
	@Autowired
	private SysUserManager sysUserManager;
	@Autowired
	private BiChannelManager channelManager;
	@Autowired
	private BiServerManager   serverManager;
	@Autowired
	private SysUserProjectRoleManager sysUserProjectRoleManager;
	@Autowired
	private SysFunctionManager sysFunctionManager;
	@Autowired
	private SysRoleFunctionManager sysRoleFunctionManager;
	@Autowired
	private SysRoleServerManager sysRoleServerManager;
	@Autowired
	private SysRoleChannelManager sysRoleChannelManager;
	private int roleId;
	private Map<Integer, String> statusMap;
	private Map<Integer, EntitySysRole> roleMap = new HashMap<Integer, EntitySysRole>();
	private List<EntitySysUser> userList = new ArrayList<EntitySysUser>();			  // userList
	private List<EntitySysFunction> functionList = new ArrayList<>();// functionList
	private List<EntityBiServer>   serverList    = new ArrayList<>(); 	  // serverList
	private List<EntityBiChannel> channelList    = new ArrayList<>();   // channelList

	@Autowired
	public void init() {
		roleMap.clear();
		roleId = 0;
		for (EntitySysRole role : sysRoleManager.findAll()) {
			roleMap.put(role.getId(), role);
		}
		userList.clear();
		serverList.clear();
		functionList.clear();
		channelList.clear();
		statusMap = sysFunctionManager.getStatusMap();
		change();
	}

	public void change()
	{
		EntitySysRole role = sysRoleManager.get(roleId);
		if (null == role)
		{
			userList.clear();
			serverList.clear();
			functionList.clear();
			channelList.clear();
			return;
		}
		// 初始化userList
		userList.clear();
		List<EntitySysUserRole> list1 = sysUserProjectRoleManager.findBy(EntitySysUserRole.K.roleId, roleId);
		if (null != list1 && !list1.isEmpty())
		{
			for (EntitySysUserRole info : list1)
			{
				EntitySysUser user = sysUserManager.get(info.getUserId());
				if (null != user)
				{
					userList.add(user);
				}
			}
		}
		// 初始化functionList
		functionList.clear();
		List<EntitySysRoleFunction> list2 = sysRoleFunctionManager.findBy(EntitySysRoleFunction.K.roleId, roleId);
		if (null != list2 && !list2.isEmpty())
		{
			for (EntitySysRoleFunction info : list2)
			{
				EntitySysFunction func = sysFunctionManager.get(info.getFunctionId());
				if (null != func)
				{
					functionList.add(func);
				}
			}
		}
		// 初始化serverList
		serverList.clear();
		List<EntitySysRoleServer> list3 = sysRoleServerManager.findBy(EntitySysRoleServer.K.roleId, roleId);
		if (null != list3 && !list3.isEmpty())
		{
			for (EntitySysRoleServer info : list3)
			{
				EntityBiServer server = serverManager.get(info.getServerId());
				if (null != server)
				{
					serverList.add(server);
				}
			}
		}
		// 初始化channelList
		channelList.clear();
		List<EntitySysRoleChannel> list4 = sysRoleChannelManager.findBy(EntitySysRoleChannel.K.roleId, roleId);
		if (null != list4 && !list4.isEmpty())
		{
			for (EntitySysRoleChannel info : list4)
			{
				EntityBiChannel channel = channelManager.get(info.getChannelId());
				if (null != channel)
				{
					channelList.add(channel);
				}
			}
		}
		sysLogManager.addLog(session.getUserName(), "查询角色的用户和功能信息 ！");
	}

	public int getRoleId()
	{
		return roleId;
	}

	public void setRoleId(int roleId)
	{
		this.roleId = roleId;
	}

	public Map<Integer, EntitySysRole> getRoleMap()
	{
		return roleMap;
	}

	public void setRoleMap(Map<Integer, EntitySysRole> roleMap)
	{
		this.roleMap = roleMap;
	}

	public Map<Integer, String> getStatusMap()
	{
		return statusMap;
	}

	public void setStatusMap(Map<Integer, String> statusMap)
	{
		this.statusMap = statusMap;
	}

	public List<EntitySysUser> getUserList()
	{
		return userList;
	}

	public void setUserList(List<EntitySysUser> userList)
	{
		this.userList = userList;
	}

	public List<EntitySysFunction> getFunctionList()
	{
		return functionList;
	}

	public void setFunctionList(List<EntitySysFunction> functionList)
	{
		this.functionList = functionList;
	}

	public List<EntityBiServer> getServerList()
	{
		return serverList;
	}

	public void setServerList(List<EntityBiServer> serverList)
	{
		this.serverList = serverList;
	}

	public List<EntityBiChannel> getChannelList()
	{
		return channelList;
	}

	public void setChannelList(List<EntityBiChannel> channelList)
	{
		this.channelList = channelList;
	}
}